home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 1865 / 1865.xpi / chrome / adblockplus.jar / content / ui / fennecOverlay.js < prev    next >
Text File  |  2010-01-07  |  5KB  |  147 lines

  1. /* ***** BEGIN LICENSE BLOCK *****
  2.  * Version: MPL 1.1
  3.  *
  4.  * The contents of this file are subject to the Mozilla Public License Version
  5.  * 1.1 (the "License"); you may not use this file except in compliance with
  6.  * the License. You may obtain a copy of the License at
  7.  * http://www.mozilla.org/MPL/
  8.  *
  9.  * Software distributed under the License is distributed on an "AS IS" basis,
  10.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  11.  * for the specific language governing rights and limitations under the
  12.  * License.
  13.  *
  14.  * The Original Code is Adblock Plus.
  15.  *
  16.  * The Initial Developer of the Original Code is
  17.  * Fabrice Desr├⌐.
  18.  * Portions created by the Initial Developer are Copyright (C) 2006-2009
  19.  * the Initial Developer. All Rights Reserved.
  20.  *
  21.  * Contributor(s):
  22.  *  Wladimir Palant
  23.  *
  24.  * ***** END LICENSE BLOCK ***** */
  25.  
  26. function onCreateOptions(event)
  27. {
  28.     if (event.originalTarget.getAttribute("addonID") != "{d10d0bf8-f5b5-c8b4-a8b2-2b9879e08c5d}")
  29.         return;
  30.  
  31.     E("adblockplus-subscription-list").addEventListener("command", function(event)
  32.     {
  33.         let menu = event.target;
  34.         if (menu.value)
  35.             setSubscription(menu.value, menu.label);
  36.     }, false);
  37.  
  38.     updateSubscriptionList();
  39.     abp.filterStorage.addSubscriptionObserver(updateSubscriptionList);
  40.  
  41.     window.addEventListener("unload", function()
  42.     {
  43.         abp.filterStorage.removeSubscriptionObserver(updateSubscriptionList);
  44.     }, false);
  45. }
  46.  
  47. function updateSubscriptionList()
  48. {
  49.     let filterStorage = abp.filterStorage;
  50.     let currentSubscription = filterStorage.subscriptions.filter(function(subscription) subscription instanceof abp.DownloadableSubscription);
  51.     currentSubscription = (currentSubscription.length ? currentSubscription[0] : null);
  52.     
  53.     let xhr = Cc["@mozilla.org/xmlextras/xmlhttprequest;1"].createInstance(Ci.nsIJSXMLHttpRequest);
  54.     xhr.open("GET", "chrome://adblockplus/content/ui/subscriptions.xml", false);
  55.     xhr.send(null);
  56.     if (!xhr.responseXML)
  57.         return;
  58.  
  59.     let menu = E("adblockplus-subscription-list");
  60.     menu.removeAllItems();
  61.  
  62.     let subscriptions = xhr.responseXML.documentElement.getElementsByTagName("subscription");
  63.     for (let i = 0; i < subscriptions.length; i++)
  64.     {
  65.         let item = subscriptions[i];
  66.         menu.appendItem(item.getAttribute("title"), item.getAttribute("url"), null);
  67.         if (currentSubscription && item.getAttribute("url") == currentSubscription.url)
  68.             menu.selectedIndex = menu.itemCount - 1;
  69.     }
  70.  
  71.     if (currentSubscription && menu.selectedIndex < 0)
  72.     {
  73.         menu.appendItem(currentSubscription.title, currentSubscription.url, null);
  74.         menu.selectedIndex = menu.itemCount - 1;
  75.     }
  76. }
  77.     
  78. function setSubscription(url, title)
  79. {
  80.     let filterStorage = abp.filterStorage;
  81.     let currentSubscription = filterStorage.subscriptions.filter(function(subscription) subscription instanceof abp.DownloadableSubscription);
  82.     currentSubscription = (currentSubscription.length ? currentSubscription[0] : null);
  83.     if (currentSubscription && currentSubscription.url == url)
  84.         return;
  85.  
  86.     // We only allow one subscription, remove existing one before adding
  87.     if (currentSubscription)
  88.         filterStorage.removeSubscription(currentSubscription);
  89.  
  90.     currentSubscription = abp.Subscription.fromURL(url);
  91.     currentSubscription.title = title;
  92.  
  93.     filterStorage.addSubscription(currentSubscription);
  94.     abp.synchronizer.execute(currentSubscription);
  95.     filterStorage.saveToDisk();
  96. }
  97.  
  98. function initFennecSubscriptionDialog(url, title)
  99. {
  100.     // Copied from Fennec's PromptService.js
  101.     // add a width style to prevent a element to grow larger 
  102.     // than the screen width
  103.     function sizeElement(id, percent)
  104.     {
  105.         let elem = E(id);
  106.         let screenW = E("main-window").getBoundingClientRect().width;
  107.         elem.style.width = screenW * percent / 100 + "px"
  108.     }
  109.     
  110.     // size the height of the scrollable message. this assumes the given element
  111.     // is a child of a scrollbox
  112.     function sizeScrollableMsg(id, percent)
  113.     {
  114.         let screenH = E("main-window").getBoundingClientRect().height;
  115.         let maxHeight = screenH * percent / 100;
  116.         
  117.         let elem = E(id);
  118.         let style = document.defaultView.getComputedStyle(elem, null);
  119.         let height = Math.ceil(elem.getBoundingClientRect().height) +
  120.                                  parseInt(style.marginTop) +
  121.                                  parseInt(style.marginBottom);
  122.     
  123.         if (height > maxHeight)
  124.             height = maxHeight;
  125.         elem.parentNode.style.height = height + "px";
  126.     }
  127.  
  128.     sizeElement("abp-subscription-title", 50);
  129.     E("abp-subscription-title").textContent = title;
  130.  
  131.     sizeElement("abp-subscription-url", 50);
  132.     E("abp-subscription-url").textContent = url;
  133.     sizeScrollableMsg("abp-subscription-url", 50);
  134.  
  135.     E("abp-subscription-cmd-ok").addEventListener("command", function()
  136.     {
  137.         setSubscription(url, title);
  138.         E("abpEditSubscription").close();
  139.     }, false);
  140.  
  141.     E("abp-subscription-btn-ok").focus();
  142. }
  143.  
  144. abp.runAsync(function() abp.init());
  145.  
  146. E("addons-list").addEventListener("AddonOptionsLoad", onCreateOptions, false);
  147.